﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Common.Extension;
using Common.Model;
using Spider.Mode;

namespace Spider.Util
{
    public class Util
    {
        public static List<News> Dxwevents2News(List<DxwEvent> events)
        {
            return events.Select(m => new News
            {
                Abs = m.Content,
                BlockClassify = m.Block,
                Content = m.Content,
                ContentId = m.EventId,
                ContentLink = "",
                CrawlingTime = DateTime.Now,
                From = "dxw",
                IsTrainingSet = 1,
                SiteTime = m.SiteTime,
                Stocks = m.Stocks,
                Title = m.Title
            }).ToList();
        }

        public static void DxwBlockMapping(List<News> news, Dictionary<string, string> dxwBlocksDic)
        {
            foreach (News data in news)
            {
                string block = data.BlockClassify;

                if (!String.IsNullOrEmpty(block) && dxwBlocksDic.ContainsKey(block))
                {
                    var value = dxwBlocksDic.Get(block);
                    data.BlockClassify = String.IsNullOrEmpty(value) ? block : value;
                }
            }
        }

        public static void DxwBlockMapping(List<DxwEvent> news, Dictionary<string, string> dxwBlocksDic)
        {
            foreach (DxwEvent data in news)
            {
                string block = data.Block;

                if (!String.IsNullOrEmpty(block) && dxwBlocksDic.ContainsKey(block))
                {
                    var value = dxwBlocksDic.Get(block);
                    data.Block = String.IsNullOrEmpty(value) ? block : value;
                }
            }
        }

        public static string GetMd5(string myString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x");
            }

            return byte2String;
        }

        public static string ByteToHexStr(byte[] bytes)
        {
            var returnStr = "";
            if (bytes == null) return returnStr;

            foreach (byte t in bytes)
                returnStr += t.ToString("X2");
            return returnStr;
        }
    }


    public class EventCompartor : IEqualityComparer<DxwEvent>
    {
        public bool Equals(DxwEvent x, DxwEvent y)
        {
            if (x == null || y == null)
                return false;
            if (x.EventId.Equals(y.EventId))
                return true;
            else
                return false;
        }

        public int GetHashCode(DxwEvent obj)
        {
            if (obj == null)
                return 0;
            else
                return obj.EventId.GetHashCode();
        }
    }

    public class BlockCompartor : IEqualityComparer<DxwBlock>
    {
        public bool Equals(DxwBlock x, DxwBlock y)
        {
            if (x == null || y == null)
                return false;
            if (x.BlockName.Equals(y.BlockName))
                return true;
            else
                return false;
        }

        public int GetHashCode(DxwBlock obj)
        {
            if (obj == null)
                return 0;
            else
                return obj.BlockName.GetHashCode();
        }
    }
}